package com.itany.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.itany.entity.User;
import com.itany.service.UserService;
import com.itany.util.ExceptionUtil;
import com.itany.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // @SentinelResource(value = "getUserList", blockHandler = "handlerException")
    @SentinelResource(value = "getUserList",blockHandlerClass = ExceptionUtil.class, blockHandler = "handlerException")
    @GetMapping
    public ResponseResult getUserList(){
        return userService.getUserList();
    }

    @GetMapping("/{id}")
    public ResponseResult getUser(@PathVariable Integer id){
        return userService.getUser(id);
    }

    @PostMapping
    public ResponseResult postUser(User user){
        return userService.postUser(user.getUsername(),user.getPassword());
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }

    @PutMapping
    public ResponseResult putUser(@RequestParam  Map<String,Object> map){ // 此处可以直接使用Map来接收参数，必须添加@RequestParam注解
        return userService.putUser(map);
    }

    /**
     * 处理异常的方法
     * @return
     */
    // public ResponseResult handlerException(BlockException e){
    //     return ResponseResult.fail("服务器正忙，请稍后再试："+e);
    // }

}
